package com.yf.catcafe.controller;

import com.yf.catcafe.common.OrderNoGenerator;
import com.yf.catcafe.pojo.dto.SeckillCafeDto;
import com.yf.catcafe.pojo.enity.Order;
import com.yf.catcafe.pojo.vo.OrderQueryVo;
import com.yf.catcafe.result.Result;
import com.yf.catcafe.service.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/catcafe")
@CrossOrigin("*")
public class OrderController {

    @Autowired
    private OrdersService ordersService;

    @GetMapping("/orders")
    public Result<List<OrderQueryVo>> query(){
        List<Order> orders=ordersService.query();
        List<OrderQueryVo> orderQueryVos=orders.stream().map(order -> {
            OrderQueryVo vo=new OrderQueryVo();
            vo.setOrderId(order.getOrderNo());
            vo.setTotal(order.getTotal());
            vo.setNote(order.getNote());
            vo.setNumber(order.getNumber());
            vo.setFlavor(order.getFlavor());
            vo.setCoffeeName(order.getCafeName());
            vo.setUserId(order.getUserId());
            vo.setTime(order.getOrderTime().toString());
            vo.setId(order.getId());
            return vo;
        }).collect(Collectors.toList());
        return Result.success(orderQueryVos);
    }

    @DeleteMapping("/orders/{id}")
    public Result delete(@PathVariable(value = "id") Long id){
        ordersService.delete(id);
        return Result.success();
    }

    @PostMapping("/ordersseckill")
    public Result seckillorders(@RequestBody SeckillCafeDto seckillCafeDto){
        ordersService.seckillinsert(seckillCafeDto);
        return Result.success();
    }
}
